package com.spring.component.test.xml.test;

import com.spring.component.utils.ReflectUtil;
import com.spring.component.xml.annotation.Level;
import com.spring.component.xml.annotation.Xml;

/**
 * @ClassName: UserXmlFactory
 * @Description:
 * @author: liushihua
 * @date: 2019/1/8
 **/
public class UserXmlFactory {

  /**
   * 根据body的实体类获取不同UserModifyReq
   * 
   * @param clazz body节点中额外的节点映射bean的class
   * @return
   * @throws NoSuchFieldException
   * @throws IllegalAccessException
   */
  public static Class<UserModifyReq> getUserXmlClass(Class<? extends Body> clazz)
      throws NoSuchFieldException, IllegalAccessException {
    Class userModifyReqClass = UserModifyReq.class;
    Level level = (Level) userModifyReqClass.getAnnotation(Level.class);
    Xml[] xmls = level.xmls();
    for (Xml xml : xmls) {
      if (xml.attrName().equals("body")) {
        ReflectUtil.changeAnnotationValue(xml, "clazz", clazz);
      }
    }
    return userModifyReqClass;
  }

  /**
   * 根据body的实体类获取不同UserModifyReq
   * 
   * @param reClazz root节点的映射bean的class
   * @param bodyClazz body节点中额外的节点映射bean的class
   * @param <T>
   * @return
   * @throws NoSuchFieldException
   * @throws IllegalAccessException
   */
  public static <T> Class<T> getXmlClass(Class<T> reClazz, Class bodyClazz)
      throws NoSuchFieldException, IllegalAccessException {
    Level level = reClazz.getAnnotation(Level.class);
    Level bodyLevel = (Level) ReflectUtil.getAnnotation(bodyClazz, Level.class);
    Xml[] xmls = level.xmls();
    for (Xml xml : xmls) {
      if (xml.xmlName().equals(bodyLevel.xmlName())) {
        ReflectUtil.changeAnnotationValue(xml, "clazz", bodyClazz);
      }
    }
    return reClazz;
  }

}
